package config

import (
	"bytes"
	"embed"
	"fmt"
	"os"

	"github.com/spf13/viper"
)

//go:embed application.*.yaml
var configs embed.FS

func init() {
	env := os.Getenv("ENV")
	if env == "" {
		env = "dev"
	}
	fmt.Printf("ENV: %s\n", env)
	vp := viper.New()
	// 根据环境变量ENV决定要读取的应用启动配置
	configFileStream, err := configs.ReadFile("application." + env + ".yaml")
	if err != nil {
		panic(err)
	}
	vp.SetConfigType("yaml")
	err = vp.ReadConfig(bytes.NewReader(configFileStream))
	if err != nil {
		panic(err)
	}

	vp.UnmarshalKey("app", &App)
	vp.UnmarshalKey("database", &Database)
	vp.UnmarshalKey("redis", &Redis)
	// Database.Master.MaxLifeTime *= time.Second
}
